**Housekeeping
clear all
cap log close
log using ${rep_root}/logs/combine_panels.log, text replace
set more off


**Create outcome lists by source file
local arrests_list arrests serious first_off serious_first_off type1_arrests
local complaints_list complaints force_complaints
local trr_list force sub_inj
local awards_list awards merit merit_valor merit_valor_routine
local yearly_list unit_change12 promoted12
local attend_list attend desk

**Load in arrest data
use ${rep_root}/data/arrests_panel

drop arrest_yr arrest_m

**Merge on complaints data
sort mid yearmo
merge 1:1 mid yearmo using ${rep_root}/data/complaints_panel, keep(1 3) keepusing(`complaints_list') gen(complaints_merge)

**Set complaints outcome for an officer/yearmonth to 0 if it isn't anywhere in the complaints data
foreach var in `complaints_list'{
	replace `var' = 0 if complaints_merge==1
}

**Merge on trr, award, salary, unithistory data
sort mid yearmo
merge 1:1 mid yearmo using ${rep_root}/data/mid_panel, keep(1 3) keepusing(app_yearmo resig_yearmo `trr_list' `awards_list' `yearly_list') gen(mid_merge)


**Merge on attendance and assignment data
sort mid yearmo
merge 1:1 mid yearmo using ${rep_root}/data/aa_panel, keep(1 3)  
 
**Trim off yearmonths outside the officer's service
sort mid
by mid: egen app_yearmo_all = min(app_yearmo)
by mid: egen resig_yearmo_all = min(resig_yearmo)

drop if yearmo<app_yearmo_all
drop if yearmo>resig_yearmo_all
drop app_yearmo* resig_yearmo*

**Set outcome for an officer/yearmonth to 0 if it isn't in the original data
foreach var in `trr_list' `awards_list' `yearly_list'{
	replace `var' = 0 if mid_merge==1 | missing(`var')
}
foreach var in `attend_list'{
	replace `var' = 0 if missing(`var') & yearmo>=ym(2008,9)
}
 

drop _merge
save ${rep_root}/data/all_panel, replace 
clear
log close
